package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constant.ApUserStatesConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserRealNameMapper;
import com.heima.user.service.ApUserRealNameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApUserRealNameServiceImpl extends ServiceImpl<ApUserRealNameMapper, ApUserRealname> implements ApUserRealNameService {


    /**
     * 审核失败
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        if (authDto==null || authDto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        update(Wrappers.<ApUserRealname>lambdaUpdate().eq(ApUserRealname::getId, authDto.getId())
                .set(ApUserRealname::getStatus, ApUserStatesConstants.CHECKED_WITH_FAIL)
                .set(ApUserRealname::getReason,authDto.getMsg()));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核通过
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto authDto) {
        if(authDto == null || authDto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        update(Wrappers.<ApUserRealname>lambdaUpdate().eq(ApUserRealname::getId, authDto.getId())
                .set(ApUserRealname::getStatus, ApUserStatesConstants.CHECKED_WITH_SUCCESS));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询用户真实信息
     *
     * @param authDto
     */
    @Override
    public ResponseResult listRealInfo(AuthDto authDto) {
        log.info("【查询用户审核】--- begin");
        log.info("【参数】:{}",authDto);
        if(authDto == null || authDto.getPage()==null || authDto.getSize()==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页查询
        IPage page = new Page(authDto.getPage(),authDto.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if(authDto.getStatus() != null){
           queryWrapper.eq(ApUserRealname::getStatus,authDto.getStatus());
        }
        page = page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(authDto.getPage(),authDto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        log.info("【查询结果】",page.getRecords());
        log.info("【查询用户审核】--- end");
        return responseResult;
    }
}
